﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using DistantWorldsModAssembler.Helpers;

namespace DistantWorldsModAssembler
{
    public partial class mainForm : Form
    {
        #region Private Variables

        private static string stamp = "'Created by " + Application.ProductName + " " + Application.ProductVersion + "\r\n";
        
        private enum raceFamily
        {
            Humaniod = 0,
            Ursidian = 1,
            Insectoid = 2,
            Reptilian = 3,
            Amphibian = 4,
            Rodent = 5,
            Machine = 6
        };

        private enum overallShipDesignFocus
        {
            Balanced = 0,
            Speed = 1,
            Power = 2,
            Efficiency = 3
        };

        #endregion

        public mainForm()
        {
            InitializeComponent();
        }

        private void mainForm_Load(object sender, EventArgs e)
        {
            this.aboutPicture.SizeMode = PictureBoxSizeMode.Zoom;

            this.newRaceFamilyCombo.DataSource = Enum.GetValues(typeof(raceFamily));

            this.newRaceReproductionRateComboBox.DataSource = ListValuesHelper.GetReproductionRate();
            this.newRaceReproductionRateComboBox.DisplayMember = "Value";

            this.newRaceIntelligenceComboBox.DataSource = ListValuesHelper.GetIntellegence();
            this.newRaceIntelligenceComboBox.DisplayMember = "Value";

            this.newRaceAggressionComboBox.DataSource = ListValuesHelper.GetAggression();
            this.newRaceAggressionComboBox.DisplayMember = "Value";

            this.newRaceCautionComboBox.DataSource = ListValuesHelper.GetCaution();
            this.newRaceCautionComboBox.DisplayMember = "Value";

            this.newRaceFriendlinessComboBox.DataSource = ListValuesHelper.GetFriendliness();
            this.newRaceFriendlinessComboBox.DisplayMember = "Value";

            this.newRaceLoyaltyComboBox.DataSource = ListValuesHelper.GetLoyalty();
            this.newRaceLoyaltyComboBox.DisplayMember = "Value";

            // ! shipsets

            this.newRaceShipMaintenanceSavingsComboBox.DataSource = ListValuesHelper.GetPercentage();
            this.newRaceShipMaintenanceSavingsComboBox.DisplayMember = "Value";

            this.newRaceTroopMaintenanceSavingsComboBox.DataSource = ListValuesHelper.GetPercentage();
            this.newRaceTroopMaintenanceSavingsComboBox.DisplayMember = "Value";

            this.newRaceResourceExtractionBonusComboBox.DataSource = ListValuesHelper.GetPercentage();
            this.newRaceResourceExtractionBonusComboBox.DisplayMember = "Value";

            this.newRaceWarWearinessAttenuationComboBox.DataSource = ListValuesHelper.GetPercentage();
            this.newRaceWarWearinessAttenuationComboBox.DisplayMember = "Value";

            this.newRaceSatisfactionModifierComboBox.DataSource = ListValuesHelper.GetPercentage();
            this.newRaceSatisfactionModifierComboBox.DisplayMember = "Value";

            this.newRaceResearchBonusComboBox.DataSource = ListValuesHelper.GetPercentage();
            this.newRaceResearchBonusComboBox.DisplayMember = "Value";

            this.newRaceEspionageBonusComboBox.DataSource = ListValuesHelper.GetPercentage();
            this.newRaceEspionageBonusComboBox.DisplayMember = "Value";

            this.newRaceTradeBonusComboBox.DataSource = ListValuesHelper.GetPercentage();
            this.newRaceTradeBonusComboBox.DisplayMember = "Value";

            this.newRaceOverallShipDesignFocusComboBox.DataSource = Enum.GetValues(typeof(overallShipDesignFocus));

        }

        #region About

        private void mainForm_DragDrop(object sender, DragEventArgs e)
        {
            int x = this.PointToClient(new Point(e.X, e.Y)).X;

            int y = this.PointToClient(new Point(e.X, e.Y)).Y;

            if (x >= this.aboutPicture.Location.X && x <= this.aboutPicture.Location.X + this.aboutPicture.Width && y >= this.aboutPicture.Location.Y && y <= this.aboutPicture.Location.Y + this.aboutPicture.Height)
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

                if (tabControl.SelectedTab.Name == "aboutTab")
                {
                    this.aboutPicture.Image = Image.FromFile(files[0]);
                }

                if (tabControl.SelectedTab.Name == "racesTab")
                {
                    this.newRacePicture.Image = Image.FromFile(files[0]);
                }
            }
        }

        private void mainForm_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void aboutSelectImageButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog aboutPictureDialog = new OpenFileDialog();
            aboutPictureDialog.Filter = "Images Files (*.*)|*.*";
            if (aboutPictureDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                this.aboutPicture.Image = Image.FromFile(aboutPictureDialog.FileName);
            }
        }

        private void aboutCreateButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.aboutNameText.Text))
            {
                MessageBox.Show(this, "Please enter name.", "Data Missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrEmpty(this.aboutText.Text))
            {
                MessageBox.Show(this, "Please enter about text.", "Data Missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (this.aboutPicture.Image == null)
            {
                MessageBox.Show(this, "Please select or drag and drop an image.", "Data Missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string basePath = @"C:\Matrix Games\Distant Worlds\Customization\";

            string path = basePath + this.aboutNameText.Text;

            if (Directory.Exists(path))
            {
                if (DialogResult.Cancel == MessageBox.Show(this, "This customization folder already exists. Click Ok to overwrite about files.", "Create About Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
                {
                    return;
                }
            }

            Directory.CreateDirectory(path);

            string aboutTextPath = basePath + this.aboutNameText.Text + @"\\about.txt";

            FileInfo fileInfoAboutText = new FileInfo(aboutTextPath);
            File.WriteAllText(fileInfoAboutText.FullName, stamp + this.aboutText.Text);

            string aboutImagePath = basePath + this.aboutNameText.Text + @"\\about.png";

            this.aboutPicture.Image.Save(aboutImagePath, System.Drawing.Imaging.ImageFormat.Png);

            MessageBox.Show(this, "Your customization folder and about files have been created.", "Create About Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        #endregion

        private void raceAddButton_Click(object sender, EventArgs e)
        {
            StringBuilder race = new StringBuilder();
            race.AppendLine(stamp);

        }

        private void newRaceSelectImageButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog newRacePictureDialog = new OpenFileDialog();
            newRacePictureDialog.Filter = "Images Files (*.*)|*.*";
            if (newRacePictureDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                this.newRacePicture.Image = Image.FromFile(newRacePictureDialog.FileName);
            }
        }

        private void newRaceGroupBox_Enter(object sender, EventArgs e)
        {

        }

        private void label16_Click(object sender, EventArgs e)
        {

        }

        private void newRaceTechFocus2ComboBox_Click(object sender, EventArgs e)
        {

        }

        private void newRaceWarWearinessAttenuationComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void newRaceFactorsFriendlinesslabel_Click(object sender, EventArgs e)
        {

        }

        private void newRaceSpecialComponetComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void newRacePeriodicChangeIntervalComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void newRaceShipSizeFactorCivilianComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}
